<?php
/**
 * InitPHP开源框架 - DEM
 * @author zhuli
 */
class indexController extends HController {

	public $initphp_list = array('getLogs','operateLogs'); //Action白名单

	private function _getStaffService() {
		return InitPHP::getService ( "staff" );
	}
	
	private function _getHolidayLogsService() {
		return InitPHP::getService ( "holidaylogs" );
	}
	
	/**
	 * 首页
	 */
	public function run() {
		$operate_user = '';
		$operate_password = $this->controller->get_get('operate_user');
		switch ($operate_password){
			case 'URlhkNYADbu6rkJMPVLkFq5qaxWZVvcX':
				$operate_user = '张耀斌';break;
			case 'uaicOuY8k8Ohq44pjxKdMN7L23MFpZiK':
				$operate_user = '徐永军';break;
			case 's4hAq1mF9l6uKaO7TbNQOTyKAxCTXkp7':
				$operate_user = '崔富振';break;
			case 'YZoTPdLlZvnlPl5DmtbkqSvmcvIPGoB5':
				$operate_user = '王宪';break;
			case '55nvxDkDVqm6vJQ9uYtnV0OblorVSaR7':
				$operate_user = '余子龙';break;
			case 'e2sMJp8f8EZOSSgbkgOyOcZsS3azebqY':
				$operate_user = '成林';break;
			case 'VrdLa8F394TDXYukcUGa8EA59BFKv8LH':
				$operate_user = '张盛';break;
			default:
				break;
		}
		
		
		$staff_data = $this->_getStaffService()->getAllStaff();
		
// 		print_r($staff_data);exit;
		
		$this->view->assign('staff_data', $staff_data);
		$this->view->assign('operate_user', $operate_user);
		$this->view->display("index/index");
	}
	
	/**
	 * 获取日志
	 */
	public function getLogs(){
		$staff_id = $this->controller->get_post('id');
		$logs = $this->_getHolidayLogsService()->getlogsByStaff($staff_id);
		if(!empty($logs)){
			$logs_list = array();
			foreach ($logs as $key=>$value){
				$holiday_type = $value['holiday_type']==1?'调休':'年假';
				$opperate_type = $value['opperate_type']==1?'减少':'增加';
				$text_str = $value['operate_user'].'给'.$value['staff_name'].$opperate_type.'了'.$value['days'].'天'.$holiday_type;
				$logs_list[$key] = array(
						'id'=>$value['id'],
						'text'=>$text_str,
						'remark'=>$value['reason'],
						'time'=>$value['operate_time'],
				);
			}
			$result = array('status'=>0,'list'=>$logs_list);		
			echo json_encode($result);exit;
		}
		else{
			$result_empty = array('status'=>0,'list'=>array(array('id'=>'999','text'=>'暂无记录','remark'=>'','time'=>'')));
			echo json_encode($result_empty);exit;
		}
		print_r($logs);exit;
	}
	
	/**
	 * 编辑操作
	 */
	public function operateLogs(){
		if($this->controller->is_ajax()){
			$logs_data = $this->controller->get_gp(array('id','selectType','select','days','remark','staff_name','user'), $type = 'post', $isfilter = true);
			$result = $this->checkValue($logs_data);
			if($result['status']){
				echo json_encode($result);exit;
			}
			$logs_datas['staff_id'] = $logs_data['id'];
			$logs_datas['holiday_type'] = $logs_data['selectType'];
			$logs_datas['opperate_type'] = $logs_data['select'];
			$logs_datas['days'] = $logs_data['days'];
			$logs_datas['reason'] = $logs_data['remark'];
			$logs_datas['operate_user'] = $logs_data['user'];
			$logs_datas['staff_name'] = $logs_data['staff_name'];
			$logs_datas['operate_time'] = date('y-m-d H:i:s',time());
	// 		print_r($logs_datas);exit;
			$result_add_logs = $this->_getHolidayLogsService()->operateHolidayLogs($logs_datas);
			if($result_add_logs){
				echo json_encode(array('status'=>0,'msg'=>'假期增减成功'));exit;
			}
			else{
				echo json_encode(array('status'=>0,'msg'=>'假期增减失败'));exit;
			}
		}
// 		print_r($result_add_logs);exit;
	}
	
	/**
	 * 检查输入项
	 * @param unknown $logs_data
	 */
	private function checkValue($logs_data){
		$result = array('status'=>0,'msg'=>'校验成功');
		if(empty($logs_data['id'])){
			$result = array('status'=>1,'msg'=>'id获取失败');
			return $result;
		}
		if(empty($logs_data['days'])){
			$result = array('status'=>1,'msg'=>'请输入调整假期天数');
			return $result;
		}
		if(!is_numeric($logs_data['days'])){
			$result = array('status'=>1,'msg'=>'假期天数必须为数字');
			return $result;
		}
		return $result;
		
	}
	
	
	
	
}